package com.snoob.springboot.bffServiceAuth.controller;

import com.snoob.springboot.base.BaseException;
import com.snoob.springboot.base.BaseResult;
import com.snoob.springboot.enums.SysKeyEnum;
import com.snoob.springboot.pipleline.dto.UserLoginDtoReq;
import com.snoob.springboot.pipleline.dto.UserLoginDtoRes;
import com.snoob.springboot.bffServiceAuth.service.EnterpriseUserService;
import com.snoob.springboot.bffServiceAuth.service.SysUserService;
import com.snoob.springboot.bffServiceAuth.util.LoginUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @描述：登陆
 * @作者：lixing_java@163.com
 * @时间：2022/5/17 21:20
 */
@RestController
@RequestMapping("login")
public class LoginController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private EnterpriseUserService enterpriseUserService;

    /**
     * @描述 用户登陆
     */
    @PostMapping("doLogin")
    public BaseResult<UserLoginDtoRes> doLogin(@RequestBody UserLoginDtoReq dto, HttpServletRequest request) throws Exception {
        // 校验登陆参数
        LoginUtil.checkLoginParam(dto);
        // 校验图像验证码
        LoginUtil.checkImgCode(dto);
        // 登陆类型：系统用户登陆、企业用户登陆
        String loginCategory = dto.getLoginCategory();
        if (loginCategory.equals(SysKeyEnum.LOGIN_CATEGORY_SYSTEM.getKey())) {
            return new BaseResult<UserLoginDtoRes>().success(sysUserService.sysUserLogin(dto, request));
        } else if (loginCategory.equals(SysKeyEnum.LOGIN_CATEGORY_ENTERPRISE.getKey())) {
            return new BaseResult<UserLoginDtoRes>().success(enterpriseUserService.enterpriseUserLogin(dto));
        } else {
            throw new BaseException("登录用户类型非法");
        }
    }
}
